package com.futvan.z.system.ztext;

import com.futvan.z.framework.core.SuperAction;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.DateUtil;
import com.futvan.z.framework.util.FileUtil;
import com.futvan.z.framework.util.SystemUtil;
import net.lingala.zip4j.ZipFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;

@Controller
public class ZtextAction extends SuperAction{
	@Autowired
	private ZtextService ztextService;

	/**
     * 生成安卓语言文件
	 * @return
     * @throws Exception
	 */
	@RequestMapping(value="/CreateTextAndroidLanguageFile")
	public ResponseEntity<byte[]> CreateTextAndroidLanguageFile() throws Exception {

		//简体中文
		StringBuffer zhcn = new StringBuffer();
		zhcn.append("<resources xmlns:tools=\"http://schemas.android.com/tools\" tools:ignore=\"ExtraTranslation\">").append("\r\n");
		//中文-繁体
		StringBuffer zhhk = new StringBuffer();
		zhhk.append("<resources xmlns:tools=\"http://schemas.android.com/tools\" tools:ignore=\"ExtraTranslation\">").append("\r\n");
		//英文
		StringBuffer en = new StringBuffer();
		en.append("<resources xmlns:tools=\"http://schemas.android.com/tools\" tools:ignore=\"ExtraTranslation\">").append("\r\n");

		//遍历所有文本
		for (z_text zText : z.textList) {
			zhcn.append("	<string name=\""+zText.getNumber()+"\">"+zText.getZhcn()+"</string>").append("\r\n");
			zhhk.append("	<string name=\""+zText.getNumber()+"\">"+zText.getZhhk()+"</string>").append("\r\n");
			en.append("	<string name=\""+zText.getNumber()+"\">"+zText.getEn()+"</string>").append("\r\n");

		}
		zhcn.append("</resources>").append("\r\n");
		zhhk.append("</resources>").append("\r\n");
		en.append("</resources>").append("\r\n");

		//生成目录
		FileUtil.mkdirs(SystemUtil.getRunWebAppPath()+"/temp/AndroidLanguage");

		//生成简体中文
		File zhcn_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/AndroidLanguage/zhcn.xml",zhcn.toString());
		//生成繁体中文
		File zhhk_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/AndroidLanguage/zhhk.xml",zhhk.toString());
		//生成英文
		File en_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/AndroidLanguage/en.xml",en.toString());

		//压缩打包
		ZipFile zipFile = new ZipFile(SystemUtil.getRunWebAppPath()+"/temp/AndroidLanguage.zip");
		zipFile.addFolder(new File(SystemUtil.getRunWebAppPath()+"/temp/AndroidLanguage"));
		byte[] file = FileUtil.getBytes(SystemUtil.getRunWebAppPath()+"/temp/AndroidLanguage.zip");

		//创建文件头信息
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", "AndroidLanguage.zip");
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(file,headers, HttpStatus.CREATED);
	}


	/**
	 * 生成IOS语言文件
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/CreateTextIOSLanguageFile")
	public ResponseEntity<byte[]> CreateTextIOSLanguageFile() throws Exception {

		//简体中文
		StringBuffer zhcn = new StringBuffer();
		zhcn.append("/* create time "+ DateUtil.getDateTime()+" */").append("\r\n");
		//中文-繁体
		StringBuffer zhhk = new StringBuffer();
		zhhk.append("/* create time "+ DateUtil.getDateTime()+" */").append("\r\n");
		//英文
		StringBuffer en = new StringBuffer();
		en.append("/* create time "+ DateUtil.getDateTime()+" */").append("\r\n");

		//遍历所有文本
		for (z_text zText : z.textList) {
			zhcn.append("\""+zText.getNumber()+"\"=\""+zText.getZhcn()+"\";").append("\r\n");
			zhhk.append("\""+zText.getNumber()+"\"=\""+zText.getZhhk()+"\";").append("\r\n");
			en.append("\""+zText.getNumber()+"\"=\""+zText.getEn()+"\";").append("\r\n");
		}

		//生成目录
		FileUtil.mkdirs(SystemUtil.getRunWebAppPath()+"/temp/IOSLanguage");

		//生成简体中文
		File zhcn_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/IOSLanguage/zhcn.strings",zhcn.toString());
		//生成繁体中文
		File zhhk_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/IOSLanguage/zhhk.strings",zhhk.toString());
		//生成英文
		File en_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/IOSLanguage/en.strings",en.toString());

		//压缩打包
		ZipFile zipFile = new ZipFile(SystemUtil.getRunWebAppPath()+"/temp/IOSLanguage.zip");
		zipFile.addFolder(new File(SystemUtil.getRunWebAppPath()+"/temp/IOSLanguage"));
		byte[] file = FileUtil.getBytes(SystemUtil.getRunWebAppPath()+"/temp/IOSLanguage.zip");

		//创建文件头信息
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", "IOSLanguage.zip");
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(file,headers, HttpStatus.CREATED);
	}

	/**
	 * 生成H5语言文件
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/CreateTextH5LanguageFile")
	public ResponseEntity<byte[]> CreateTextH5LanguageFile() throws Exception {

		//简体中文
		StringBuffer zhcn = new StringBuffer();
		zhcn.append("{").append("\r\n");
		//中文-繁体
		StringBuffer zhhk = new StringBuffer();
		zhhk.append("{").append("\r\n");
		//英文
		StringBuffer en = new StringBuffer();
		en.append("{").append("\r\n");

		//遍历所有文本
		for (int i = 0; i < z.textList.size(); i++) {
			z_text zText = z.textList.get(i);
			if(i==z.textList.size()-1){
				zhcn.append("\""+zText.getNumber()+"\":\""+zText.getZhcn()+"\"").append("\r\n");
				zhhk.append("\""+zText.getNumber()+"\":\""+zText.getZhhk()+"\"").append("\r\n");
				en.append("\""+zText.getNumber()+"\":\""+zText.getEn()+"\"").append("\r\n");
			}else{
				zhcn.append("\""+zText.getNumber()+"\":\""+zText.getZhcn()+"\",").append("\r\n");
				zhhk.append("\""+zText.getNumber()+"\":\""+zText.getZhhk()+"\",").append("\r\n");
				en.append("\""+zText.getNumber()+"\":\""+zText.getEn()+"\",").append("\r\n");
			}
		}

		zhcn.append("}").append("\r\n");
		zhhk.append("}").append("\r\n");
		en.append("}").append("\r\n");

		//生成目录
		FileUtil.mkdirs(SystemUtil.getRunWebAppPath()+"/temp/H5Language");

		//生成简体中文
		File zhcn_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/H5Language/zhcn.json",zhcn.toString());
		//生成繁体中文
		File zhhk_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/H5Language/zhhk.json",zhhk.toString());
		//生成英文
		File en_file = FileUtil.CreaterTXT(SystemUtil.getRunWebAppPath()+"/temp/H5Language/en.json",en.toString());

		//压缩打包
		ZipFile zipFile = new ZipFile(SystemUtil.getRunWebAppPath()+"/temp/H5Language.zip");
		zipFile.addFolder(new File(SystemUtil.getRunWebAppPath()+"/temp/H5Language"));
		byte[] file = FileUtil.getBytes(SystemUtil.getRunWebAppPath()+"/temp/H5Language.zip");

		//创建文件头信息
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", "H5Language.zip");
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(file,headers, HttpStatus.CREATED);
	}
}
